﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

//Following namespace is required for Forms Authentication
using System.Web.Security;
using System.Configuration;

namespace EWDT_Lab_Demo
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        private void Page_Init(object sender, EventArgs e)
        {
            //This event does nothing. It is meant to highlight the life cycle of a ASP.NET page
            bool temp = false;
            if (temp) { }
        }

        private void Page_PreRender(object sender, EventArgs e)
        {
            //This event does nothing. It is meant to highlight the life cycle of a ASP.NET page
            bool temp = false;
            if (temp) { }         
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Let's check if the user has logged on and display the appropriate greeting and controls to the user.
                if (Context.User.Identity.Name != "")
                {
                    pnlLogin.Visible = false;
                    pnlLogout.Visible = true;
                    lblStatus.Text = "Hello " + (string)Session["ewdt_login"] + "!!! Welcome to the KESAMET Online System.";
                    Menu1.Items[1].Enabled = true;
                }
                else
                {
                    pnlLogin.Visible = true;
                    pnlLogout.Visible = false;
                    lblStatus.Text = "";
                    Menu1.Items[1].Enabled = false;
                }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (tbxUserID.Text == "user" && tbxPassword.Text == "password")
            {
                //User has been successfully authenticated.
                FormsAuthentication.RedirectFromLoginPage(tbxUserID.Text, false);

                //Authenticated successfully, store the user's fullname in the session("ewdt_login")
                //Assume the user's fullname is Tan Ah Kow
                Session.Add("ewdt_login", "Tan Ah Kow");
            }
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            //Remove the session("ewdt_login") since the user has logged out
            Session.Remove("ewdt_login");

            //Sign out the user
            FormsAuthentication.SignOut();

            //Redirect the user back to the DEFAULT webpage 
            Response.Redirect("default.aspx");
        }
    }
}